using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCNoun : RepositoryItemUI
	{
		#region Initialization

		public UCNoun(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			InitializeComponent();

			init();
		}
		private void init()
		{
			DataTable dt = Global.Data.GetDataTypes();
			cboDataType.BeginUpdate();
			cboDataType.Items.Clear();
			foreach ( DataRow row in dt.Rows ) {
				cboDataType.Items.Add(row["datatype"].ToString());
			}
			cboDataType.EndUpdate();

			cmbWhitespace.Items.Add(Noun.WhiteSpaceHandling.preserve);
			cmbWhitespace.Items.Add(Noun.WhiteSpaceHandling.replace);
			cmbWhitespace.Items.Add(Noun.WhiteSpaceHandling.collapse);
		}

		#endregion

		#region Public Members

		#region Properties
		#endregion

		#region Events
		#endregion

		#region Methods
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private Noun _noun = null;
		#endregion

		#region Methods
		private void load()
		{
			if ( _noun == null ) return;

			txtGVarName.Text = _noun.Name;
			txtGVarDesc.Text = _noun.Description;
			cboDataType.Text = _noun.DataType;

			cboDataType.Visible = ( _noun.Type == Noun.NounType.Noun );
			lblDataType.Visible = ( _noun.Type == Noun.NounType.Noun );

			if ( string.IsNullOrEmpty(cboDataType.Text) ) {
				cboDataType.SelectedIndex = cboDataType.FindString("string");
			}
			tvUsedByHirarchy.RepositoryItem = _noun;

			lbEnum.DataSource = _noun.Properties.Enumeration;
			if ( _noun.Properties.Length != null ) {
				cboLength.Checked = true;
				numLength.Value = (decimal)_noun.Properties.Length;
			} else {
				cboLength.Checked = false;
				numLength.Value = 0;
			}
			if ( _noun.Properties.MinLength != null ) {
				cboMinLength.Checked = true;
				numMinLength.Value = (decimal)_noun.Properties.MinLength;
			} else {
				cboMinLength.Checked = false;
				numMinLength.Value = 0;
			}
			if ( _noun.Properties.MaxLength != null ) {
				cboMaxLength.Checked = true;
				numMaxLength.Value = (decimal)_noun.Properties.MaxLength;
			} else {
				cboMaxLength.Checked = false;
				numMaxLength.Value = 0;
			}
			if ( _noun.Properties.Pattern != null ) {
				cboPattern.Checked = true;
				txtPattern.Text = _noun.Properties.Pattern;
			} else {
				cboPattern.Checked = false;
				txtPattern.Text = "";
			}
			if ( _noun.Properties.WhiteSpace != null ) {
				cboWhitespace.Checked = true;
				cmbWhitespace.SelectedItem = _noun.Properties.WhiteSpace;
			} else {
				cboWhitespace.Checked = false;
				cmbWhitespace.SelectedItem = null;
			}
			if ( _noun.Properties.FractionDigits != null ) {
				cboFractionDigits.Checked = true;
				numFractionDigits.Value = (decimal)_noun.Properties.FractionDigits;
			} else {
				cboFractionDigits.Checked = false;
				numFractionDigits.Value = 0;
			}
			if ( _noun.Properties.TotalDigits != null ) {
				cboTotalDigits.Checked = true;
				numTotalDigits.Value = (decimal)_noun.Properties.TotalDigits;
			} else {
				cboTotalDigits.Checked = false;
				numTotalDigits.Value = 0;
			}
			if ( _noun.Properties.MinInclusive != null ) {
				cboMinInclusive.Checked = true;
				numMinInclusive.Value = (decimal)_noun.Properties.MinInclusive;
			} else {
				cboMinInclusive.Checked = false;
				numMinInclusive.Value = 0;
			}
			if ( _noun.Properties.MaxInclusive != null ) {
				cboMaxInclusive.Checked = true;
				numMaxInclusive.Value = (decimal)_noun.Properties.MaxInclusive;
			} else {
				cboMaxInclusive.Checked = false;
				numMaxInclusive.Value = 0;
			}
			if ( _noun.Properties.MinExclusive != null ) {
				cboMinExclusive.Checked = true;
				numMinExclusive.Value = (decimal)_noun.Properties.MinExclusive;
			} else {
				cboMinExclusive.Checked = false;
				numMinExclusive.Value = 0;
			}
			if ( _noun.Properties.MaxExclusive != null ) {
				cboMaxExclusive.Checked = true;
				numMaxExclusive.Value = (decimal)_noun.Properties.MaxExclusive;
			} else {
				cboMaxExclusive.Checked = false;
				numMaxExclusive.Value = 0;
			}
		}
		private void save()
		{
			if ( _noun == null ) _noun = new Noun();

			_noun.Reload();

			_noun.Name = txtGVarName.Text;
			_noun.Description = txtGVarDesc.Text;
			_noun.DataType = cboDataType.Text;

			_noun.Properties.Length = ( cboLength.Checked ? (uint?)numLength.Value : null );
			_noun.Properties.MinLength = ( cboMinLength.Checked ? (uint?)numMinLength.Value : null );
			_noun.Properties.MaxLength = ( cboMaxLength.Checked ? (uint?)numMaxLength.Value : null );

			_noun.Properties.Pattern = ( cboPattern.Checked ? txtPattern.Text : null );

			_noun.Properties.WhiteSpace = ( cboWhitespace.Checked ? (Noun.WhiteSpaceHandling?)cmbWhitespace.SelectedItem : null );

			_noun.Properties.FractionDigits = ( cboFractionDigits.Checked ? (uint?)numFractionDigits.Value : null );
			_noun.Properties.TotalDigits = ( cboTotalDigits.Checked ? (uint?)numTotalDigits.Value : null );
			_noun.Properties.MinInclusive = ( cboMinInclusive.Checked ? (uint?)numMinInclusive.Value : null );
			_noun.Properties.MaxInclusive = ( cboMaxInclusive.Checked ? (uint?)numMaxInclusive.Value : null );
			_noun.Properties.MinExclusive = ( cboMinExclusive.Checked ? (uint?)numMinExclusive.Value : null );
			_noun.Properties.MaxExclusive = ( cboMaxExclusive.Checked ? (uint?)numMaxExclusive.Value : null );
		}
		#endregion

		#region Event Handlers
		#endregion

		#endregion

		protected override void NotifyPropertyChange(string property, object value)
		{
			if ( property == "Name" && value != null ) {
				txtGVarName.Text = value.ToString();
			}
		}

		public override IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _noun;
			}
			set
			{
				base.RepositoryItem = value;
				if ( value is Noun ) {
					_noun = (Noun)value;
					load();
				}
			}
		}

		public override string TypeText
		{
			get { return Resources.sNoun; }
		}

		public override string TypeDescription
		{
			get { return ""; }
		}

		private void txtGVarName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( e.KeyChar < (char)32 ) return;
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		private void cbo_CheckedChanged(object sender, EventArgs e)
		{
			CheckBox cbo = sender as CheckBox;
			if ( cbo == null ) throw new NullReferenceException();
			Control next = this.GetNextControl(cbo, true);
			next.Enabled = cbo.Checked;
		}

		private void txtEnum_TextChanged(object sender, EventArgs e)
		{
			btnAdd.Enabled = ( txtEnum.TextLength > 0 );
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			_noun.Properties.Enumeration.Add(txtEnum.Text);
			txtEnum.Text = "";
		}

		private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if ( lbEnum.SelectedItem is string ) {
				_noun.Properties.Enumeration.Remove((string)lbEnum.SelectedItem);
			}
		}

		private void deleteAllToolStripMenuItem_Click(object sender, EventArgs e)
		{
			_noun.Properties.Enumeration.Clear();
		}

	}
}
